package cate.common.table.item.hero.row;

import cate.common.table.EExcelNoteField;
import cate.common.table.ERow;
import cate.common.table.XSSFRowWrap;
import cate.common.table.d.GDCamp;
import cate.common.table.d.GDHero;
import cate.common.table.item.IBagItemTpl;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("英雄基础")
public class HeroBaseRow extends ERow implements IBagItemTpl {

  @NoteField("原型ID")
  public int protoId;

  @NoteField("下一星英雄ID")
  public int nextStarId;

  @NoteField("英雄名")
  public String name;

  @NoteField("职业")
  public byte job;

  @NoteField("阵营")
  public byte camp;

  @NoteField("强度")
  public int strength;

  @NoteField("性别")
  public byte gender;

  @NoteField("初始属性")
  public String attrInitStr;

  @NoteField("星级")
  public int star;

  @NoteField("资质")
  public int zizhi;

  @NoteField("阶数上限")
  public int gradeMax;

  @NoteField("等级上限")
  public int levelMax;

  @NoteField("基础技能")
  private String skillStr;

  @NoteField("图鉴展示")
  public boolean display;

  @NoteField("属性等级成长")
  public String growStr;

  @NoteField("消耗指定英雄")
  public String cost0;

  @NoteField("消耗狗粮")
  public String cost1;

  @NoteField("消耗材料")
  public String cost3;

  @NoteField("真实英雄")
  public boolean real;

  @NoteField("是否一键造出")
  public boolean gmProducible;

  @NoteField("突破等级上限")
  public byte canBreakLimit;

  @NoteField("升星分段")
  public boolean isNeedSeg;

  @NoteField("食物名备注")
  public String foodNameNote;

  @NoteField("升星固定属性")
  public String intrinsicStr;
  @NoteField("星级属性")
  private String starScaleStr;
  @NoteField("进阶系数")
  private String gradeScaleStr;
  @NoteField("进阶固定属性")
  public String gradeAttrStr;

  @EExcelNoteField(fieldName = "资质", defaultValue = "80")
  public int talent;

  @EExcelNoteField(fieldName = "升星类型", defaultValue = "1")
  public byte upType;

  @EExcelNoteField(fieldName = "挂接升星任务", defaultValue = "")
  public String taskStr;

  @EExcelNoteField(fieldName = "觉醒技能", defaultValue = "")
  public String taskSkillStr;

  @EExcelNoteField(fieldName = "灵契属性", defaultValue = "")
  public String magicAttr;


  public int worldLevelRequire;

  @EExcelNoteField(fieldName = "品阶")
  public int quality;


  @Override
  public String getName() {
    return name;
  }

  @Override
  public int getTemplateId() {
    return id;
  }

  @Override
  public long getPileMax() {
    return 1;
  }

  @Override
  public void parse(XSSFRowWrap xrw) {
    super.parse(xrw);
    this.id = xrw.getInteger("id", 0);
    this.protoId = xrw.getInteger("原型id", 0);
    this.nextStarId = xrw.getInteger("下一星id", 0);
    this.name = xrw.getString("英雄名");
    this.star = xrw.getInteger("星级", 0);
    this.zizhi = xrw.getInteger("资质", 0);
    this.gradeMax = xrw.getInteger("阶数上限", 0);
    this.levelMax = xrw.getInteger("等级上限", 0);
    this.skillStr = xrw.getString("基础技能");
    this.display = xrw.getInteger("图鉴展示", 0) == 1;
    this.growStr = xrw.getString("属性等级成长");
    this.cost0 = xrw.getString("消耗指定英雄");
    this.cost1 = xrw.getString("消耗狗粮");
    this.cost3 = xrw.getString("消耗材料");
    this.real = xrw.getBoolean("真实英雄", true);
    this.gmProducible = xrw.getBoolean("是否一键造出", false);
    this.canBreakLimit = xrw.getByte("突破等级上限", (byte) 0);
    this.isNeedSeg = xrw.getBoolean("升星分段", false);
    this.intrinsicStr = xrw.getString("升星固定属性");
    this.starScaleStr = xrw.getString("星级系数");
    this.gradeScaleStr = xrw.getString("进阶系数");
    this.gradeAttrStr = xrw.getString("进阶固定属性");
    this.job = xrw.getByte("职业", GDHero.Job.NULL);
    this.camp = xrw.getByte("阵营", GDCamp.Type.NULL);
    this.attrInitStr = xrw.getString("初始属性");
    this.strength = xrw.getInteger("英雄强度", 1);
    this.gender = xrw.getByte("性别", GDHero.Sex.MALE);
    this.foodNameNote = xrw.getString("食物名备注");
    this.worldLevelRequire = xrw.getInteger("共鸣等级需求", 0);
  }

  @Override
  public boolean available() {
    return this.real;
  }

  /**
   * 技能们
   */
  public List<Integer> skillList;
  /**
   * 技能们
   */
  public List<Integer> taskSkillList;

  public String cost = "";

  public String needSelectCost = "";

  /**
   * 升星任务任务 map<第几个任务,任务ID>
   */
  public Map<Integer, Integer> taskMap = new HashMap<>();
  public Map<Integer, Double> gradeScaleMap = new HashMap<>();
  public Map<Integer, Double> starScaleMap = new HashMap<>();

  @Override
  public void decode() {
    if (upType != GDHero.UpType.NORMAL) {
      int index = 1;
      if (StringUtils.isNotBlank(taskStr)) {
        for (String s : taskStr.split("#")) {
          taskMap.put(index, Integer.valueOf(s));
          index++;
        }
      }
    }

    skillList = new ArrayList<>();
    if (StringUtils.isNotBlank(skillStr)) {
      for (String s : skillStr.split("#")) {
        skillList.add(Integer.valueOf(s));
      }
    }
    taskSkillList = new ArrayList<>();
    if (StringUtils.isNotBlank(taskSkillStr)) {
      for (String s : taskSkillStr.split("#")) {
        taskSkillList.add(Integer.valueOf(s));
      }
    }
    if (!StringUtils.isBlank(cost0) && !"0".equals(cost0)) {
      cost += cost0;
      needSelectCost += "#" + cost0;
    }
    if (!StringUtils.isBlank(cost1) && !"0".equals(cost1)) {
      cost += "#" + cost1;
      needSelectCost += "#" + cost1;
    }
    if (!StringUtils.isBlank(cost3) && !"0".equals(cost3)) {
      cost += "#" + cost3;
    }

    if (StringUtils.isNotBlank(gradeScaleStr)) {
      for (String str : gradeScaleStr.split("#")) {
        String[] arr = str.split(":");
        gradeScaleMap.put(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]) / 10000d);
      }
    }

    if (StringUtils.isNotBlank(starScaleStr)) {
      for (String str : starScaleStr.split("#")) {
        String[] arr = str.split(":");
        starScaleMap.put(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]) / 100000d);
      }
    }
  }

  public boolean isBreakLimit() {
    return this.canBreakLimit == 1;
  }

  @Override
  public String toName() {
    return this.foodNameNote;
  }

  @Override
  public boolean unique() {
    return true;
  }

  @Override
  public String toString() {
    return "" + this.name + "(id=" + this.id + ")";
  }
}
